package com.lancoo.znbkxx.teacher.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.view.isVisible
import com.lancoo.znbkxx.teacher.Ext.Companion.GLOBAL_GRADE
import com.lancoo.znbkxx.teacher.Ext.Companion.PLATFORM_URL
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.base.SharedPreferencesHelper
import com.lancoo.znbkxx.teacher.databinding.ActivityHelpBinding
import com.lancoo.znbkxx.teacher.ui.Presenter
import com.lancoo.znbkxx.teacher.ui.dialog.PrivacyDialog
import com.lancoo.znbkxx.teacher.viewmodel.UserInfoViewModel
//import com.tencent.smtt.sdk.WebChromeClient
//import com.tencent.smtt.sdk.WebSettings
//import com.tencent.smtt.sdk.WebView
//import com.tencent.smtt.sdk.WebViewClient
import kotlinx.android.synthetic.main.activity_help.*


/**
 * @author : Admin
 * @date   : 2019/9/24 14:28
 * @desc   :
 */
class HelpActivity : BaseVMActivity<UserInfoViewModel, ActivityHelpBinding>() {
    override fun providerVMClass(): Class<UserInfoViewModel>? {
        return UserInfoViewModel::class.java
    }

    override fun setLayoutID(): Int {
        return R.layout.activity_help
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initView() {
        binding.sbtnBack.setOnClickListener { v ->
            if (wv.canGoBack()) run {
                wv.goBack()
                if (binding.sbtnAgree.isVisible) {
                    binding.sbtnAgree.clearAnimation()
                    binding.sbtnAgree.visibility = View.GONE
                }
            } else {
                finish()
            }
        }

        binding.sbtnAgree.clearAnimation()
        binding.sbtnAgree.visibility = View.GONE
        binding.sbtnAgree.setOnClickListener {
            showPermissionsDialog()
        }

        binding.wv.apply {
            settings.javaScriptEnabled = true
            settings.useWideViewPort = true
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            settings.loadWithOverviewMode = true
            webChromeClient = WebChromeClient()
            wv.setLayerType(View.LAYER_TYPE_HARDWARE, null)
            wv.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(webView: WebView?, s: String): Boolean {
                    if (s.contains("tel:400-698-6166")) {
                        callPhone()
                        return true
                    }
                    return super.shouldOverrideUrlLoading(webView, s)
                }

                override fun onPageFinished(webView: WebView, s: String) {
                    super.onPageFinished(webView, s)
                    Log.e("onPageFinished：", "===========url===========$s")
                    if (s.contains("xieyi-s.html") || s.contains("yinsi-s.html")) {
                        val isAgreePermissions = SharedPreferencesHelper.getBoolean("isAgreePermissions", false)
                        if (isAgreePermissions) {
                            binding.sbtnAgree.clearAnimation()
                            binding.sbtnAgree.visibility = View.GONE
                        } else {
                            binding.sbtnAgree.clearAnimation()
                            binding.sbtnAgree.visibility = View.VISIBLE
                        }
                    }
                    if (s.contains("tel:400-698-6166")) {
                    }
                }
            }
        }
        var schoolType = 0
        val juniorList = arrayOf("K6","K7", "K8", "K9")
        val seniorList = arrayOf("K10", "K11", "K12")
        if (juniorList.contains(GLOBAL_GRADE)) {
            schoolType = 1
        }
        if (seniorList.contains(GLOBAL_GRADE)) {
            schoolType = 2
        }
        binding.wv.loadUrl("${PLATFORM_URL}Web/UserHelp/NewBookEdit_Junior_Mobile.html?userType=1&schoolType=$schoolType")
    }

    private var privacyDialog: PrivacyDialog? = null
    private fun showPermissionsDialog() {
        privacyDialog = PrivacyDialog(this, this, true)
        privacyDialog!!.setPresenter(object : Presenter {
            override fun onClick(view: View?) {
                SharedPreferencesHelper.applyBoolean("isAgreePermissions", true)
                privacyDialog!!.dismiss()
                binding.sbtnAgree.clearAnimation()
                binding.sbtnAgree.visibility = View.GONE
            }

            override fun onCancel(view: View?) {
                SharedPreferencesHelper.applyBoolean("isAgreePermissions", false)
                privacyDialog!!.dismiss()
            }
        }).show()

    }


    override fun onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack()
            if (binding.sbtnAgree.isVisible) {
                binding.sbtnAgree.clearAnimation()
                binding.sbtnAgree.visibility = View.GONE
            }
        } else {
            finish()
        }
    }

    private fun callPhone() {
        val intent = Intent(Intent.ACTION_DIAL)
        val data = Uri.parse("tel:" + "4006986166")
        intent.data = data
        startActivity(intent)
    }
}